package com.lifang.imgsoa.util;

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.imageio.ImageIO;

import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.Thumbnails.Builder;
import net.coobird.thumbnailator.geometry.Position;
import net.coobird.thumbnailator.geometry.Positions;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ThumbnailUtil {
	private static Logger logger = LoggerFactory.getLogger(ThumbnailUtil.class);

	//压缩质量
	private static final float QUALITY = 0.5f;
	//水印透明度
	private static final float OPACITY = 1.0f;
	//位置
	private static final Position POSITION = Positions.CENTER;
	
	/**
	 * @date 2014年5月26日 下午4:18:30
	 * @param bigImageFile 原图
	 * @param scale 比例  (//scale(0.25f)//scale(1.10f))
	 * @param thumbNailFile 缩略图
	 * @throws IOException 
	 * 
	 * @description  
	 * 按照比例缩放大小（true），
	 * 
	 * 按照比例缩放图片
	 * 
	 */
	public static File getThumbnail(File bigImageFile, double scale, File thumbNailFile) throws IOException {
		try {
			//scale(0.25f)//scale(1.10f)
			Thumbnails.of(bigImageFile).scale(scale).outputQuality(QUALITY).toFile(thumbNailFile);
		}
		catch (IOException e) {
			logger.info(e.getMessage());
			e.printStackTrace();
			throw e;
		}
		return thumbNailFile;
	}

	public static OutputStream getThumbnail_stream(InputStream inputStream, double scale, OutputStream outputStream) throws IOException {
		try {
			//scale(0.25f)//scale(1.10f)
			Thumbnails.of(inputStream).scale(scale).outputQuality(QUALITY).toOutputStream(outputStream);
			inputStream.close();
		}
		catch (IOException e) {
			logger.info(e.getMessage());
			e.printStackTrace();
			throw e;
		}
		return outputStream;
	}

	/**
	 * 
	 * 功能描述:裁剪图片比例
	 *
	 *@author Andy:   2015年3月30日      新建
	 *
	 * @param inputStream
	 * @param outputStream
	 * @param img
	 * @return
	 * @throws IOException
	 */
	public static OutputStream getCutImage(InputStream inputStream, OutputStream outputStream, ImgEnum img) throws IOException {
		return getCutImage(inputStream, outputStream, img.getWidth(), img.getHeight());
	}

	/**
	 * 
	 * 功能描述:按照指定宽度和高度进行剪裁
	 *
	 * <pre>
	 * Modify Reason:(修改原因,不需覆盖，直接追加.)
	 *     Andy:   2015年3月30日      新建
	 * </pre>
	 *
	 * @param inputStream
	 * @param outputStream
	 * @param width
	 * @param height
	 * @return
	 * @throws IOException
	 */
	public static OutputStream getCutImage(InputStream inputStream, OutputStream outputStream, int width, int height) throws IOException {
		BufferedImage image = ImageIO.read(inputStream);
		Builder builder = null;

		int originalWidth = image.getWidth();//原图宽度
		int originalHeight = image.getHeight();//原图高度

		int newWidth = width;//新图宽度
		int newHeight = height;//新图高度

		if ((float) newWidth / newHeight != (float) originalWidth / originalHeight) {
			if ((float) newWidth / newHeight < (float) originalWidth / originalHeight) {
				//原图比较宽
				image = Thumbnails.of(image).height(newHeight).asBufferedImage();
			}else {
				//原图比较高
				image = Thumbnails.of(image).width(newWidth).asBufferedImage();
			}
			builder = Thumbnails.of(image).sourceRegion(Positions.CENTER, newWidth, newHeight).size(newWidth, newHeight);
		}else {
			builder = Thumbnails.of(image).size(newWidth, newHeight);
		}
		builder.outputFormat("jpg").outputQuality(0.9f).toOutputStream(outputStream);
		return outputStream;
	}

	public static OutputStream doWateRmark(InputStream inputStream, File wateRmarkFile, OutputStream outputStream, Position position,int width, int height) throws IOException {
		try {
			Thumbnails.of(inputStream)
				.size(width, height)
				.watermark(position, ImageIO.read(wateRmarkFile), OPACITY).outputQuality(QUALITY)
				.toOutputStream(outputStream);
			inputStream.close();
		}
		catch (IOException e) {
			logger.info(e.getMessage());
			e.printStackTrace();
			throw e;
		}
		return outputStream;
	}

	/**
	 * 
	 * 功能描述:获取缩放之后添加的水印图片
	 *
	 * @author Andy:   2015年3月27日 
	 *
	 * @param inputStream
	 * @param waterFile
	 * @param outputStream
	 * @return
	 * @throws IOException
	 */
	public static OutputStream getWaterFile(InputStream inputStream, File waterFile, OutputStream outputStream, ImgEnum imgEnum) throws IOException {
		return doWateRmark(inputStream, waterFile, outputStream, POSITION, imgEnum.getWidth(), imgEnum.getHeight());
	}
	/**
	 * 
	 * 功能描述:获取缩放之后添加的水印图片(水印在左上角)
	 *
	 * @author Andy:   2015年4月23日      新建
	 *
	 * @param inputStream
	 * @param waterFile
	 * @param outputStream
	 * @return
	 * @throws IOException
	 */
	public static OutputStream getWaterFileTopLeft(InputStream inputStream, File waterFile, OutputStream outputStream, ImgEnum imgEnum) throws IOException {
		return doWateRmark(inputStream, waterFile, outputStream, Positions.TOP_LEFT, imgEnum.getWidth(), imgEnum.getHeight());
	}
	/**
	 * 功能描述:获取缩放之后添加的水印图片(水印在右下角)
	 *
	 * <pre>
	 * Modify Reason:(修改原因,不需覆盖，直接追加.)
	 *     Andy:   2015年4月23日      新建
	 * </pre>
	 *
	 * @param inputStream
	 * @param waterFile
	 * @param outputStream
	 * @param imgEnum
	 * @return
	 * @throws IOException
	 */
	public static OutputStream getWaterFileBottomRight(InputStream inputStream, File waterFile, OutputStream outputStream, ImgEnum imgEnum) throws IOException {
		return doWateRmark(inputStream, waterFile, outputStream, Positions.BOTTOM_RIGHT, imgEnum.getWidth(), imgEnum.getHeight());
	}

	/**
	 * 
	 * 功能描述:获取添加水印之后的图片
	 *
	 * @author Andy:   2015年3月30日      新建
	 *
	 * @param fileBytes
	 * @param waterFile
	 * @param outputStream
	 * @return
	 * @throws IOException
	 */
	public static OutputStream getWaterFile(byte[] fileBytes, File waterFile, OutputStream outputStream) throws IOException {
		try {
			ByteArrayInputStream inputStream = new ByteArrayInputStream(fileBytes);
			BufferedImage bufferedImage = ImageIO.read(inputStream);
			return getWaterFile(fileBytes, waterFile, outputStream, bufferedImage.getWidth(), bufferedImage.getHeight());
		}
		catch (IOException e) {
			logger.info(e.getMessage());
			e.printStackTrace();
			throw e;
		}
	}
	public static OutputStream getWaterFile(byte[] fileBytes, File waterFile, OutputStream outputStream,int width,int height) throws IOException {
		return getWaterFile(fileBytes, waterFile,Positions.CENTER, outputStream, width, height);
	}
	public static OutputStream getWaterFileTopLeft(byte[] fileBytes, File waterFile, OutputStream outputStream,int width,int height) throws IOException {
		return getWaterFile(fileBytes, waterFile,Positions.TOP_LEFT, outputStream, width, height);
	}
	public static OutputStream getWaterFileBottomRight(byte[] fileBytes, File waterFile, OutputStream outputStream,int width,int height) throws IOException {
		return getWaterFile(fileBytes, waterFile,Positions.BOTTOM_RIGHT, outputStream,width, height);
	}

	public static OutputStream getWaterFile(byte[] fileBytes, File waterFile,Positions positions,OutputStream outputStream,int width,int height) throws IOException{
		try {
			ByteArrayInputStream inputStream = new ByteArrayInputStream(fileBytes);
			Thumbnails.of(inputStream)
				.size(width, height)
				.watermark(positions, ImageIO.read(waterFile), OPACITY).outputQuality(0.6f)
				.toOutputStream(outputStream);
			inputStream.close();
		}
		catch (IOException e) {
			logger.info(e.getMessage());
			e.printStackTrace();
			throw e;
		}
		return outputStream;
	}
	/**
	 * @date 2014年5月26日 下午4:18:30
	 * @param bigImageFile 原图
	 * @param thumbNailFile 缩略图
	 * @throws IOException 
	 * 
	 * @description  
	 * 按照比例缩放大小（true），
	 * 
	 * 获得指定大小的图片
	 * 
	 * 若图片横比200小，高比300小，不变
	 * 若图片横比200小，高比300大，高缩小到300，图片比例不变 
	 * 若图片横比200大，高比300小，横缩小到200，图片比例不变
	 * 若图片横比200大，高比300大，图片按比例缩小，横为200或高为300
	 * 
	 */
	public static OutputStream getThumbnailStream(InputStream inputStream, OutputStream outputStream, ImgEnum imgEnum) throws IOException {
		return getThumbnailStream(inputStream, outputStream, imgEnum.getWidth(), imgEnum.getHeight());
	}

	public static OutputStream getThumbnailStream(InputStream inputStream, OutputStream outputStream, int width, int height) throws IOException {
		try {
			//size(宽度, 高度)
			Thumbnails.of(inputStream).size(width, height).outputQuality(QUALITY).toOutputStream(outputStream);
			inputStream.close();
		}
		catch (IOException e) {
			logger.info("生成缩略图错误：{}", e.getMessage());
			e.printStackTrace();
			throw e;
		}
		return outputStream;
	}
	/**
	 * 
	 *对原图进行旋转矫正
	 *
	 * <pre>
	 * Modify Reason:(修改原因,不需覆盖，直接追加.)
	 *     Andy:   2015年4月18日      新建
	 * </pre>
	 *
	 * @param fileBytes
	 * @return
	 * @throws IOException
	 */
	public static ByteArrayOutputStream getByteArrayOutputStream(byte[] fileBytes) throws IOException{
		BufferedImage image = ImageIO.read(new ByteArrayInputStream(fileBytes));
		ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
		ThumbnailUtil.getThumbnailStream(new ByteArrayInputStream(fileBytes), byteArrayOutputStream, image.getWidth(), image.getHeight());
		return byteArrayOutputStream;
	}

	public static void main(String[] args) throws IOException {
		//		ThumbnailUtil.getThumbnail_1920(new File("D:\\temp\\whh\\1.jpg"), new File("D:\\temp\\whh\\4.jpg"));
		//		ThumbnailUtil.getThumbnail_1920(new File("G:/images/img2.jpg"), new File("G:/images/img2-2.jpg"));
		//ThumbnailUtil.getWateRmarkLeftBottom_1920(new File("G:/images/img2.jpg"), new File("G:/images/watermark.png"), new File("G:/images/img2-2.jpg"));
		File w1 = new File("C:\\Users\\Andy\\Desktop\\water\\left-up-logo.png");
		File w2 = new File("C:\\Users\\Andy\\Desktop\\water\\right-bottom-logo.png");
		File img = new File("C:\\Users\\Andy\\Desktop\\water\\pic.png");
		
		File newImg = new File("C:\\Users\\Andy\\Desktop\\water\\new_pic.png");
		InputStream inputStream = new FileInputStream(img);
		
		//添加第一张水印
		Thumbnails.of(inputStream).size(640, 640)
		.watermark(Positions.TOP_LEFT, ImageIO.read(w1), 1).outputQuality(1)
		.toFile(newImg);;
		inputStream.close();
		
		//添加第二张水印
		File newImg2 = new File("C:\\Users\\Andy\\Desktop\\water\\new_pic.png");
		inputStream = new FileInputStream(newImg2);
		
		Thumbnails.of(inputStream).size(640, 640)
		.watermark(Positions.BOTTOM_RIGHT, ImageIO.read(w2), 1).outputQuality(1)
		.toFile(newImg2);;
		inputStream.close();
		
		
	}
}
